package exttable.testfiller;

import dbutils.DbUtils;
import exttable.dbclasses.DbFile;
import exttable.view.ViewAnalysis;
import exttable.view.ViewInputFormComponents;
import utils.Localisation;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 29, 2010
 * Time: 11:45:43 AM
 * To change this template use File | Settings | File Templates.
 */
public class InputFormDisplay {

    static JPanel resultPanel;
    private int probId;
    private static int groupSchId;
    private static int visit;
    static JFrame frame;
    private JButton continueButton;
    private JScrollPane verticalPane;
    private static ViewAnalysis viewAnalysis;
    Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();

    private SpringLayout layout = new SpringLayout();

    public static DbFile dbFile;

    public static File selectedFile;

    public static int getGroupSchId() {
        return groupSchId;
    }

    public static int getVisit() {
        return visit;
    }

    public static ViewAnalysis getViewAnalysis() {
        return viewAnalysis;
    }

    public InputFormDisplay(ViewAnalysis viewAnalysis, int probId, int groupSchId, int visit, Vector<Vector<Object>> rowData) throws Exception {
        this.viewAnalysis = viewAnalysis;
        this.probId = probId;
        this.groupSchId = groupSchId;
        this.visit = visit;
        if (rowData != null && rowData.size() > 0) {
            this.rowData.addAll(rowData);
        }
        createGUI();
    }


    private void createGUI() throws Exception {
        frame = new JFrame(Localisation.getMessage("title.fillform")+" "+ DbUtils.ModeOfOperation);
        resultPanel = new JPanel();
        resultPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        verticalPane = new JScrollPane();
        final List<ViewInputFormComponents> components = new ComponentGenerator().getComponent(viewAnalysis, rowData);

        continueButton = new JButton(Localisation.getMessage("form.continue"));
        continueButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                for (ViewInputFormComponents res : components) {
                    if (!res.getCheckBox().isSelected()) {
                        res.componentValidator();
                    }
                }

                boolean errorPresent = false;
                for (ViewInputFormComponents res : components) {
                    if (res.isEmptyValue() & !res.getCheckBox().isSelected()) {
                        JOptionPane.showMessageDialog(null, Localisation.getMessage("error.emptyfileds"), Localisation.getMessage("error.title"), JOptionPane.WARNING_MESSAGE);
                        markMissedFields(components);
                        errorPresent = true;
                        break;
                    }
                }
                if (!errorPresent) {
                    if (rowData.size() == 0) {
                        new ResultPanel(new ValueRetriver().MapGenerator(components), viewAnalysis.getAnalysisName(), probId, groupSchId, visit, viewAnalysis, "NEW").getResultFrame();
                    } else {
                        new ResultPanel(new ValueRetriver().MapGenerator(components), viewAnalysis.getAnalysisName(), probId, groupSchId, visit, viewAnalysis, "EDIT").getResultFrame();
                    }

                }
            }
        });

        JLabel inputFormLabel = new JLabel(Localisation.getMessage("title.fillform"));

        frame.addWindowFocusListener(new WindowAdapter() {
            public void windowGainedFocus(WindowEvent e) {
                frame.requestFocusInWindow();
            }
        });

        organiseLayout(components);
        resultPanel.setPreferredSize(new Dimension(700, components.size() * 50));
        verticalPane.setViewportView(resultPanel);
//        verticalPane.setPreferredSize(new Dimension(1200,600));
        Dimension dimension=Toolkit.getDefaultToolkit().getScreenSize();
        verticalPane.setPreferredSize(new Dimension(dimension.width-150,dimension.height-150));

        frame.add(verticalPane);
        frame.add(continueButton);
        organiseLayout();
        frame.setVisible(true);
//        frame.setSize(1240, 720);
//        frame.setLocation(0,0);
        frame.setSize(dimension);
    }

    private void organiseLayout() {
        frame.setLayout(layout);

        layout.putConstraint(SpringLayout.WEST, verticalPane, 20, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, verticalPane, 10, SpringLayout.SOUTH, frame);

//        layout.putConstraint(SpringLayout.EAST, verticalPane, 1200, SpringLayout.EAST, frame);
//        layout.putConstraint(SpringLayout.SOUTH, verticalPane, 500, SpringLayout.NORTH, frame);

        layout.putConstraint(SpringLayout.WEST, continueButton, 10, SpringLayout.WEST, frame);
        layout.putConstraint(SpringLayout.NORTH, continueButton, 10, SpringLayout.SOUTH, verticalPane);
    }

    private void organiseLayout(Collection<? extends ViewInputFormComponents> components) {
        SpringLayout layout = new SpringLayout();
        resultPanel.setLayout(layout);
        int i = 5;
        for (ViewInputFormComponents res : components) {
            resultPanel.add(res.getLabel());
            resultPanel.add(res.getComponent());
            resultPanel.add(res.getCheckBox());
        }

        for (ViewInputFormComponents res : components) {
            int y = 0;
            if (res.getComponent().getClass().getSimpleName().equals("JScrollPane")) {
                i += 50;
                y = 1200;
            } else {
                y = 1000;
            }

//            lable
            layout.putConstraint(SpringLayout.WEST, res.getLabel(), 5, SpringLayout.WEST, resultPanel);
            layout.putConstraint(SpringLayout.NORTH, res.getLabel(), i, SpringLayout.NORTH, resultPanel);
//            component
            layout.putConstraint(SpringLayout.WEST, res.getComponent(), 700, SpringLayout.WEST, resultPanel);
            layout.putConstraint(SpringLayout.NORTH, res.getComponent(), i, SpringLayout.NORTH, resultPanel);
//            checkbox
            layout.putConstraint(SpringLayout.WEST, res.getCheckBox(), y, SpringLayout.WEST, resultPanel);
            layout.putConstraint(SpringLayout.NORTH, res.getCheckBox(), i, SpringLayout.NORTH, resultPanel);
            i += 50;
            if (res.getComponent().getClass().getSimpleName().equals("JScrollPane")) {
                i += 50;
            }
        }
    }

    private void markMissedFields(Iterable<? extends ViewInputFormComponents> components) {
        for (ViewInputFormComponents res : components) {
            if (res.isEmptyValue()) {
                res.getLabel().setForeground(Color.RED);
            } else {
                res.getLabel().setForeground(Color.BLACK);
            }
        }
    }
}
